# coding:utf-8
import sys
from PyQt5 import uic
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QMainWindow


class Calc(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        self.x = None  # 操作数
        self.y = None  # 被操作数
        self.operator = None  # 操作符
        self.do_result = None  # 计算结果

        uic.loadUi('calc.ui', self)  # ui动作识别
        btns = [
            self.pushButton0,
            self.pushButton1,
            self.pushButton2,
            self.pushButton3,
            self.pushButton4,
            self.pushButton5,
            self.pushButton6,
            self.pushButton7,
            self.pushButton8,
            self.pushButton9,
            self.pushButton_clear,
            self.pushButton_add,
            self.pushButton_sub,
            self.pushButton_div,
            self.pushButton_mul,
            self.pushButton_dot,
            self.pushButton_equal
        ]
        for btn in btns:
            btn.clicked.connect(self.click_on_button)

    def click_on_button(self):
        """主要思想：
        每次点击按钮时，
        先判断结果是否有值，
        再判断y是否有值
        再判断运算符是否有值
        再判断x是否有值
        """
        value = self.sender().text()
        if value in "0123456789":
            print "点击按钮前：", self.x, self.operator, self.y, self.do_result
            if self.do_result:  # 判断结果如果有值
                self.x = value
                self.text_result.setText(self.x)
                self.do_result = None
                self.operator = None
                self.y = None
            elif self.y:  # 结果没有值时，判断y是如果有值
                self.y += value
                self.text_result.setText(self.y)
            elif self.operator:  # y如果没有有值，判断operator如果有值
                self.y = value
                self.text_result.setText(self.y)
            elif self.x:  # 运算符如果没有，判断x如果有值
                self.x += value
                self.text_result.setText(self.x)
            else:  # x如果没有值
                self.x = value
                self.text_result.setText(self.x)
            print "点击按钮后：", self.x, self.operator, self.y, self.do_result

        elif value in "+-*/":
            print "点击按钮前：", self.x, self.operator, self.y, self.do_result
            if self.do_result:  # 判断结果如果有值
                self.x = self.do_result
                self.operator = value
                self.text_result.setText(self.operator)
                self.do_result = None
                self.y = None
            elif self.y:  # y如果没有有值，判断operator如果有值
                s = "%s%s%s" % (self.x, self.operator, self.y)
                if float(self.y) == 0.0 and self.operator == '/':       #运算符是除法，除数为0时
                    self.text_result.setText('除数不能为0')
                    self.x = None
                    self.y = None
                    self.operator = None
                    self.do_result = None
                else:
                    self.do_result = str(eval(s))
                    self.text_result.setText(self.do_result)
                self.x = self.do_result
                self.y = None
                self.operator = value
                self.do_result = None
            elif self.x:  # 运算符如果没有，判断x如果有值
                self.operator = value
                self.text_result.setText(self.operator)
            else:  # x如果没有值
                self.x = '0'
                self.operator = value
                self.text_result.setText(self.operator)
            print "点击按钮后：", self.x, self.operator, self.y, self.do_result

        elif value in '=':
            if self.do_result:
                self.x = self.do_result
                s = "%s%s%s" % (self.x, self.operator, self.y)
                if float(self.y) == 0.0 and self.operator == '/':       #运算符是除法，除数为0时
                    self.text_result.setText('除数不能为0')
                    self.x = None
                    self.y = None
                    self.operator = None
                    self.do_result = None
                else:
                    self.do_result = str(eval(s))
                    self.text_result.setText(self.do_result)
            elif self.y:
                s = "%s%s%s" % (self.x, self.operator, self.y)
                if float(self.y) == 0.0 and self.operator == '/':       #运算符是除法，除数为0时
                    self.text_result.setText('除数不能为0')
                    self.x = None
                    self.y = None
                    self.operator = None
                    self.do_result = None
                else:
                    self.do_result = str(eval(s))
                    self.text_result.setText(self.do_result)
            elif self.operator:
                self.y = self.x
                s = "%s%s%s" % (self.x, self.operator, self.y)
                if float(self.y) == 0.0 and self.operator == '/':       #运算符是除法，除数为0时
                    self.text_result.setText('除数不能为0')
                    self.x = None
                    self.y = None
                    self.operator = None
                    self.do_result = None
                else:
                    self.do_result = str(eval(s))
                    self.text_result.setText(self.do_result)
            elif self.x:
                self.text_result.setText(self.x)

        elif value in 'C':
            self.x = None  # 操作数
            self.y = None  # 被操作数
            self.operator = None  # 操作符
            self.do_result = None  # 计算结果
            self.text_result.setText('0')

        elif value in '.':
            if self.do_result:
                self.x = '0.'
                self.do_result = None
                self.y = None
                self.operator = None
                self.text_result.setText(self.x)
            elif self.y:
                if '.' in self.y:
                    pass
                else:
                    self.y += value
                    self.text_result.setText(self.y)
            elif self.operator:
                self.y = '0.'
                self.text_result.setText(self.y)
            elif self.x:
                if '.' in self.x:
                    pass
                else:
                    self.x += value
                    self.text_result.setText(self.x)
            else:
                self.x = '0.'
                self.text_result.setText(self.x)


if __name__ == '__main__':
    try:
        app = QApplication(sys.argv)
        calc = Calc()
        calc.show()
        app.exec_()
    except Exception, e:
        print e
